package com.ruoyi.system.utils;

import com.ruoyi.system.domain.ResultVO;
import com.ruoyi.system.domain.YsyTokenPojo;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.config.CacheConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * @ClassName: CacheUtil
 * @Description: ehcache
 * @Author: liu
 * @Date: 2020/11/18 10:42
 */
@Component
public class CachetokenUtil {
    @Autowired
    YsyToken ysyToken;

    private String accessToken;
    private String expireTime;
    /**
     * 获取Cache，当Cache不存在时自动创建
     *
     * @param cacheName
     * @return Cache * @author netwild@qq.com
     */
    public Cache getOrAddCache(String cacheName) {
        CacheManager cacheManager = CacheManager.create(this.getClass().getResourceAsStream("/ehcache/ehcache-shiro.xml"));//URL是指配置文件所在路径 的URL，通常使用getClass().getResource("/config/ehcache/ehcache-test.xml")获取
        Cache cache = cacheManager.getCache(cacheName);
        if (cache == null) {
            cacheManager.addCacheIfAbsent(cacheName);
            cache = cacheManager.getCache(cacheName);
//            CacheConfiguration cacheConfiguration = cache.getCacheConfiguration();
//            cacheConfiguration.setMaxEntriesLocalHeap(1000);
//            cacheConfiguration.setEternal(true);
//            ResultVO resultVO = ysyToken.getToken();
//            HashMap hashMap = (HashMap) resultVO.getData();
//            System.out.println(hashMap.get("expireTime"));
//            System.out.println(hashMap.get("accessToken"));
//            Element element = new Element("accessToken",hashMap.get("accessToken"));
//            Element element1  = new Element("expireTime",hashMap.get("expireTime"));
//            cache.put(element);
//            cache.put(element1);
//            System.out.println("缓存进去的过期时间"+cache.get("expireTime"));
//            System.out.println("缓存进去的token"+cache.get("accessToken"));
            return cache;
        }
        return cache;
    }

    public Element getYsyToken(Cache cache){
        if (cache.isKeyInCache("accessToken") && null != cache.getQuiet("accessToken")){
            System.out.println("存在的token缓存："+cache.get("accessToken"));
            Element accessToken = cache.get("accessToken");
            return accessToken;
        }else {
            cache.evictExpiredElements();
            ResultVO resultVO = ysyToken.getToken();
            HashMap hashMap = (HashMap) resultVO.getData();
            YsyTokenPojo ysyTokenPojo = new YsyTokenPojo();
            ysyTokenPojo.setAccessToken(String.valueOf(hashMap.get("accessToken")));
            ysyTokenPojo.setExpireTime((Long) hashMap.get("expireTime"));
            Long s = (ysyTokenPojo.getExpireTime() - System.currentTimeMillis()) / (1000 * 60);
//            System.out.println("当前时间戳："+System.currentTimeMillis());
//            System.out.println("相隔分钟数(需提前一小时过期)："+s);
            int seconds = (int)(s-60)*60;
//            System.out.println("秒数："+seconds);
//            System.out.println("时间戳"+ysyTokenPojo.getExpireTime());
//            Date d = new Date(ysyTokenPojo.getExpireTime());
//            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            System.out.println("当前时间："+sf.format(new Date()));
//            System.out.println("萤石云token过期时间："+sf.format(d));
            Element element = new Element("accessToken",ysyTokenPojo.getAccessToken());
            element.setEternal(false);
            element.setTimeToIdle(0);
            element.setTimeToLive(seconds);
            cache.put(element);
            Element accessToken = cache.get("accessToken");
//            System.out.println("缓存进去的token"+cache.get("accessToken"));
            //            YsyTokenPojo ysyTokenPojo = resultVO.getData();
//            System.out.println(hashMap.get("expireTime"));
//            System.out.println(hashMap.get("accessToken"));
//            Long s = ((long)hashMap.get("expireTime") - System.currentTimeMillis()) / (1000 * 60);
            return accessToken;
        }
    }
}
